//
//  CategoryDataSource.m
//  Personal Passlock
//
//  Created by 杨岑 on 5/20/15.
//  Copyright (c) 2015 杨岑. All rights reserved.
//

#import "CategoryDataSource.h"
#import "CategoryDataItem.h"
#import "CategoryRowVC.h"


@implementation CategoryDataSource
{
    NSNumberFormatter* _countFormatter;
}

+ (instancetype)create
{
    return [[CategoryDataSource alloc] init];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        _countFormatter = [[NSNumberFormatter alloc] init];
        _countFormatter.hasThousandSeparators = YES;
        _itemList = [NSMutableArray array];
    }
    return self;
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return self.itemList.count;
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    if (row >= self.itemList.count) return nil;
    
    CategoryDataItem* item = self.itemList[row];
    if (item.isGroup)
    {
        // Use row view
    }
    else
    {
        NSTableCellView* cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:tableView];
        if ([tableColumn.identifier isEqualTo:@"category"])
        {
            cellView.textField.stringValue = item.title;
            cellView.imageView.image = item.image;
        }
        else
        {
            cellView.textField.stringValue = [_countFormatter stringFromNumber:@(item.count)];
        }
        return cellView;
    }
    return nil;
}

- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
    if (row >= self.itemList.count) return nil;
    
    CategoryDataItem* item = self.itemList[row];
    // Item starting with : is a special group
    if (item.isGroup)
    {
        CategoryRowVC* vc = [[CategoryRowVC alloc] initWithNibName:@"CategoryRowView" bundle:nil];
        [vc loadView];
        vc.titleTextField.stringValue = item.title;
        
        NSTableRowView* rowView = [[NSTableRowView alloc] init];
        [rowView addSubview:vc.view];
        //vc.view.frame = rowView.bounds;
        //vc.view.autoresizingMask = NSViewMinXMargin | NSViewMinYMargin | NSViewWidthSizable | NSViewHeightSizable | NSViewMaxXMargin | NSViewMaxYMargin;
        
        NSDictionary *viewsDictionary = @{@"v": vc.view};
        NSMutableArray* constraints = [NSMutableArray array];
        [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[v]-0-|" options:0 metrics:nil views:viewsDictionary]];
        
        [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[v]-0-|" options:0 metrics:nil views:viewsDictionary]];
        vc.view.translatesAutoresizingMaskIntoConstraints = NO;
        
        [rowView addConstraints:constraints];

        return rowView;
    }
    return nil;
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
    if ([self.delegate respondsToSelector:@selector(tableViewSelectionDidChange:)])
    {
        [self.delegate tableViewSelectionDidChange:notification];
    }
}

- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row
{
    if (row < self.itemList.count)
    {
        CategoryDataItem* item = self.itemList[row];
        return !item.isGroup;
    }
    return NO;
}

@end
